from .base import BaseHandler
import sys
sys.path.append("..")
from db import sess
from models import User
import json
from views.func_tool import sms,get_random,get_email



# 发短信
class Get_code(BaseHandler):
    def get(self, *args, **kwargs):
        self.write('code码')
    def post(self, *args, **kwargs):
        phone = self.get_argument('phone')
        sms(phone)


# 发邮箱
class Get_email(BaseHandler):
    def get(self, *args, **kwargs):
        self.write('get_email')
    def post(self, *args, **kwargs):
        email = self.get_argument('email')
        get_email(email)

# 注册
class Register(BaseHandler):
    def post(self, *args, **kwargs):
        result = {}
        account = self.get_argument('account')
        pwd1 = self.get_argument('pwd1')
        pwd2 = self.get_argument('pwd1')
        gender = self.get_argument('gender')
        email = self.get_argument('email')
        phone = self.get_argument('phone')
        ma = self.get_argument('sms')
        address = self.get_argument('address')
        social = self.get_argument('social')
        u = sess.query(User).filter(User.account == account).first()
        if u:
            result['code'] = 404
            result['data'] = '用户已存在'
            self.write(json.dumps(result,ensure_ascii=False,indent=4))
        else:
            if pwd1 == pwd2:
                if gender == '男' or gender == '女':
                    if email:
                        if phone:
                            if ma == get_random():
                                if address:
                                    if social:
                                        print('社交')
                                        obj = User(account=account,
                                                   password=pwd1,
                                                   gender=gender,
                                                   email=email,
                                                   phone=phone,
                                                   address=address,
                                                   social=social
                                                   )
                                        print('添加成功')
                                        sess.add(obj)
                                        sess.commit()
                                        result['code'] = 200
                                        result['data'] = '成功！'
                                        self.write(json.dumps(result,ensure_ascii=False,indent=4))
                                    else:
                                        result['code'] = 404
                                        result['data'] = '社交不正确'
                                        self.write(json.dumps(result, ensure_ascii=False, indent=4))
                                else:
                                    result['code'] = 404
                                    result['data'] = '地址不正确'
                                    self.write(json.dumps(result, ensure_ascii=False, indent=4))
                            else:
                                result['code'] = 404
                                result['data'] = '验证码不正确'
                                self.write(json.dumps(result, ensure_ascii=False, indent=4))
                        else:
                            result['code'] = 404
                            result['data'] = '电话不正确'
                            self.write(json.dumps(result, ensure_ascii=False, indent=4))
                    else:
                        result['code'] = 404
                        result['data'] = '邮箱不正确'
                        self.write(json.dumps(result, ensure_ascii=False, indent=4))
                else:
                    result['code'] = 404
                    result['data'] = '性别不正确'
                    self.write(json.dumps(result, ensure_ascii=False, indent=4))
            else:
                result['code'] = 404
                result['data'] = '两次密码不一致'
                self.write(json.dumps(result,ensure_ascii=False,indent=4))


# 登陆
class Login(BaseHandler):
    def get(self, *args, **kwargs):
        self.render('../templates/login.html')
    def post(self, *args, **kwargs):
        result = {}
        account = self.get_argument('account')
        password = self.get_argument('password')
        print(account,password)
        u = sess.query(User).filter(User.account == account).first()
        if u:
            if u.password == password:
                result['code'] = 200
                result['data'] = '成功！'
                self.write(json.dumps(result,ensure_ascii=False,indent=4))
                # self.write(json.dumps({"status":0,"msg":"返回成功","user_info":Person.__json__(p)},ensure_ascii=False,indent=4))
            else:
                result['code'] = 404
                result['data'] = '密码错误'
                self.write(json.dumps(result,ensure_ascii=False,indent=4))
        else:
            result['code'] = 404
            result['data'] = '用户不存在'
            self.write(json.dumps(result,ensure_ascii=False,indent=4))